<template>
  <ChatRoom :messages="messages" :input="input" :setInput="setInput" :onSend="sendMessage" :title="'AI旅游大师'" :chatId="chatId" />
</template>

<script setup>
import { ref } from 'vue'
import ChatRoom from '../components/ChatRoom.vue'
import { chatWithLoveAppSse } from '../api/chat'
import { v4 as uuidv4 } from 'uuid'

const chatId = uuidv4()
const messages = ref([])
const input = ref('')

const setInput = (val) => { input.value = val }

const sendMessage = (msg) => {
  const now = Date.now()
  messages.value.push({ role: 'user', content: msg, time: now })
  chatWithLoveAppSse(msg, chatId, (chunk) => {
    if (messages.value[messages.value.length - 1].role === 'ai') {
      messages.value[messages.value.length - 1].content += chunk
      messages.value[messages.value.length - 1].time = Date.now()
    } else {
      messages.value.push({ role: 'ai', content: chunk, time: Date.now() })
    }
  })
}
</script> 